package com.sun.controller;

import com.sun.bean.Book;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class Demo1 {

    @RequestMapping(value = "/test1")
    public String test1(String username){
        System.out.println("username="+username);
        return "success";
    }

    @RequestMapping(value="/test2")
    public String testRequestParam(
            @RequestParam(value="user") String username,
            @RequestParam(value="age",required=false,defaultValue="0") int age){

        System.out.println("testRequestParam - username="+username +",age="+age);
        return "success";
    }

    @RequestMapping("/test3")
    public String test3(
            @RequestHeader("Accept-Language") String al
            ){
        System.out.println("Accept-Language="+al);
        return "success";
    }

    @RequestMapping("/test4")
    public String test4(
            @CookieValue("JSESSIONID")String cookie
    ){
        System.out.println("cookie JSESSIONID="+cookie);
        return "success";
    }

    @RequestMapping("/test5")
    public String test5(Book book){
        System.out.println(book);
        return "success";
    }

    @RequestMapping("/test6")
    public String test6(HttpSession session, HttpServletRequest request){
        request.setAttribute("requestParam","我是request域的参数");
        session.setAttribute("session","我是session域中的参数");
        return "success";
    }
}
